﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using StudentStatistics;
using Helper;
using System.Windows.Media.Animation;

namespace StudentStatistics
{
    /// <summary>
    /// StudentAchievementControl.xaml 的交互逻辑
    /// </summary>
    public partial class StudentAchievementControl : UserControl
    {

        public static StudentAchievementControl Instance
        {
            get;
            set;
        }
        public ObservableCollection<StudentData> Items
        {
            get { return (ObservableCollection<StudentData>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<StudentData>), typeof(StudentAchievementControl), new PropertyMetadata(new ObservableCollection<StudentData>()));
        public ObservableCollection<StudentData> orderItem = new ObservableCollection<StudentData>();

        private bool orderByRate = false;
        /// <summary>
        /// 当前页数
        /// </summary>
        private int CurrentPage = 0;
        /// <summary>
        /// 每页显示多少数据
        /// </summary>
        public int PageCount = 30;

        AminationHelpers amination = new AminationHelpers();

        public StudentAchievementControl()
        {
            Instance = this;
            InitializeComponent();

            //var mousesliderhelper = new MouseSliderHelper(this) { IsVerctor = true };
            //mousesliderhelper.MouseAction += mousesliderhelper_MouseAction;

            int a = 100;
            Random r = new Random(0);
            for (int i = 1; i < a; i++)
            {
                var d = (int)(r.NextDouble() * 100);
                this.orderItem.Add(new StudentData { Name = i + "_" + d.ToString(), Rate = d / 100d, ConsumptionTime = i.ToString() + "\"" });
            }

            this.IsVisibleChanged += StudentAchievementControl_IsVisibleChanged;
        }

        void StudentAchievementControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                InitPresentView();
                amination.SetAmination(this.listview, new PropertyPath(Control.HeightProperty), 0d, 690d);
                BringToTop();
            }
        }

        void mousesliderhelper_MouseAction(object sender, MouseType e)
        {
            switch (e.Type)
            {
                case ActionType.Up:
                    {
                        InitFrontView();
                        // e.MouseArgs.Handled = true;
                        break;
                    }

                case ActionType.Down:
                    {
                        InitNextView();
                        //e.MouseArgs.Handled = true;
                        break;
                    }

                default: break;
            }
        }

        private void OrderBtn_Click(object sender, RoutedEventArgs e)
        {
            var items = this.Items.OrderBy((Func<StudentData, object>)((i) => orderByRate ? i.Rate.ToString() : i.Name));
            this.Items = new ObservableCollection<StudentData>(items);
            orderByRate = !orderByRate;
        }
        /// <summary>
        /// 显示当前页
        /// </summary>
        public void InitPresentView()
        {
            this.Items = orderItem;// new ObservableCollection<StudentData>(orderItem.Skip(CurrentPage * PageCount).Take(PageCount).ToList());
        }

        /// <summary>
        /// 显示上一页
        /// </summary>
        public void InitFrontView()
        {
            CurrentPage--;
            if (CurrentPage < 0)
            {
                CurrentPage = 0;
            }
            // this.Items = orderItem;// new ObservableCollection<StudentData>(orderItem.Skip(CurrentPage * PageCount).Take(PageCount).ToList());
        }

        /// <summary>
        /// 显示下一页
        /// </summary>
        public void InitNextView()
        {
            if ((CurrentPage + 1) * PageCount <= orderItem.Count)
            {
                CurrentPage++;
            }
            //  this.Items = orderItem;// new ObservableCollection<StudentData>(orderItem.Skip(CurrentPage * PageCount).Take(PageCount).ToList());
        }

        public void BringToTop()
        {
            this.listview.ScrollIntoView(this.Items.FirstOrDefault());
        }

    }
}
